package org.example.linkedList;

/**
 * @Auther: wangbw
 * @Date:2020/11/12
 * @Description: org.example.linkedList
 * @version: 1.0
 */
public class RL {
    public static void main(String[] args) {
        System.out.println("直接修改原链表， 先进行递归反转， 然后进行插头法反转");
        Node node = Node.createNode(5);
        Node.print(node);
        Node reversalRecursion = reversalRecursion(node);
        Node.print(reversalRecursion);
        Node reversal = reversal(reversalRecursion);
        Node.print(reversal);
    }

    private static Node reversal(Node node) {
        if (node == null || node.getNext() == null)
            return node;

        Node pre = null;
        Node cur = node;
        while (cur != null){
            Node next = cur.getNext();
            cur.setNext(pre);
            pre = cur;
            cur = next;
        }
        return pre;
    }

    private static Node reversalRecursion(Node node) {
        if (node == null || node.getNext() == null)
            return node;
        Node next = node.getNext();
        Node temp = reversalRecursion(next);
        next.setNext(node);
        node.setNext(null);
        return temp;


    }
}
